<template>
    <el-main>
        <el-row :gutter="20">
            <el-card class="box-card" style="min-height: 200px;">
                <avue-crud ref="crud" :data="reportListData" :option="option" :page="page" @refresh-change="refresh"
                    @size-change="sizeChange" @current-change="currentChange"
                    @search-change="searchChange" @search-reset="searchReset">
                    <template slot-scope="scope" slot="menu">
                        <el-button :size="scope.size" :type="scope.type" v-if="scope.row.status === 0" @click="handleExam(scope.row, 1)">通过</el-button>
                        <el-button :size="scope.size" :type="scope.type" v-if="scope.row.status === 0" @click="handleExam(scope.row, 2)">撤销</el-button>
                    </template>
                </avue-crud>
            </el-card>
        </el-row>
    </el-main>
</template>

<script>
    export default {
        data() {
            return {
                searchForm: {
                    keyword: '',
                    status: '',
                },
                page: {
                    currentPage: 1,
                    total: 0,
                    pageSize: 10
                },
                reportListData: [],
                option: {
                    page: true,
                    align: 'center',
                    menuAlign: 'center',
                    size: 'small',
                    dialogWidth: '35%',
                    dialogTop: 200,
                    delBtn: false,
                    editBtn: false,
                    addBtn: false,
                    viewBtn: true,
                    searchShow: true,
                    filterBtn: false,
                    columnBtn: false,
                    searchMenuSpan: 5,
                    column: [{
                        label: '状态',
                        prop: 'status',
                        span: 24,
                        type: 'select',
                        search: true,
                        dicData: [{
                            label: '待审核',
                            value: 0
                        }, {
                            label: '举报通过',
                            value: 1
                        }, {
                            label: '举报撤销',
                            value: 2
                        }]
                    }, {
                        label: '问题标题',
                        prop: 'question_title',
                        span: 24,
                    }, {
                        label: '问题内容',
                        prop: 'question_content',
                        span: 24,
                        type: 'textarea',
                        overHidden: true,
                    }, {
                        label: '举报类型',
                        prop: 'report_type',
                        span: 24,
                        type: 'textarea',
                        overHidden: true,
                    }, {
                        label: '举报描述',
                        prop: 'report_descrption',
                        span: 24,
                        type: 'textarea',
                        overHidden: true,
                    }, {
                        label: '创建时间',
                        prop: 'created_at',
                        addDisplay: false,
                        editDisplay: false,
                        span: 24,
                        width: 140
                    }, {
                        label: '关键字',
                        prop: 'keyword',
                        addDisplay: false,
                        editDisplay: false,
                        viewDisplay: false,
                        search: true,
                        span: 24,
                        width: 180,
                        hide: true,
                        searchPlaceholder: '问题标题'
                    }]
                },
            }
        },
        created() {
            this.reportManagerList()
        },
        methods: {
            searchChange(params, done) {
                done();
                this.searchForm.keyword = params.keyword || ''
                this.searchForm.status = params.status
                this.reportManagerList()
            },
            searchReset() {
                this.searchForm.keyword = ''
                this.searchForm.status = ''
                this.reportManagerList()
            },
            // 分页
            sizeChange(val) {
                this.page.currentPage = 1
                this.page.pageSize = val
                this.reportManagerList()
            },
            // 当前页
            currentChange(val) {
                this.page.currentPage = val
                this.reportManagerList()
            },
            // 刷新
            refresh() {
                this.reportManagerList()
            },
            reportManagerList() {
                const _this = this
                const params = {
                    keyword: _this.searchForm.keyword,
                    status: _this.searchForm.status,
                    page: _this.page.currentPage,
                    limit: _this.page.pageSize,
                }
                _this.axios.post(_this.$API.ZHIHUAPI.reportManagerList, _this.qs.stringify(params)).then((res) => {
                    res = res.data
                    if (res.code === 10000) {
                        if (res.hasOwnProperty('data') && Object.keys(res.data).length > 0) {
                            _this.reportListData = res.data.list
                            _this.page.total = res.data.count
                        } else {
                            _this.reportListData = []
                            _this.page.total = 0
                        }
                    } else {
                        _this.$message({
                            showClose: false,
                            message: res.message,
                            type: 'error'
                        })
                        _this.reportListData = []
                        _this.page.total = 0
                    }
                }).catch(() => {
                    _this.$message({
                        showClose: false,
                        message: '网络连接失败，请重试！',
                        type: 'error'
                    })
                    _this.reportListData = []
                    _this.page.total = 0
                })
            },
            handleExam(row, status) {
                var _this = this

                this.$prompt('请输入审核意见', '提示', {
                  confirmButtonText: '提交',
                  cancelButtonText: '取消',
                }).then(({ value }) => {
                    if (value === null || value === 'null' || value === '') {
                        return this.$message({
                            type: 'error',
                            message: '审核意见不能为空'
                          });
                    }
                    const params = {
                        id: row.id,
                        status: status,
                        examMessage: value
                    }
                    _this.handleAxiosFun(_this.$API.ZHIHUAPI.reportManagerExam, params)

                }).catch(() => {});
            },
            handleAxiosFun(url, params, done, loading) {
                const _this = this
                _this.axios.post(url, _this.qs.stringify(params)).then((res) => {
                    if (res.data.code === 10000) {
                        _this.$message({
                            showClose: false,
                            message: res.data.message,
                            type: 'success'
                        })
                        _this.refresh()
                        if (done) {
                            done()
                        }
                    } else {
                        _this.$message({
                            showClose: false,
                            message: res.data.message,
                            type: 'error'
                        })
                        if (loading) {
                            loading()
                        }
                    }
                }).catch(() => {
                    _this.$message({
                        showClose: false,
                        message: '网络连接失败，请重试！',
                        type: 'error'
                    })
                    if (loading) {
                        loading()
                    }
                })
            }
        }
    }
</script>

<style>
</style>
